import {Modal, Tag} from 'antd';
import React, {useRef} from 'react';
import {PageContainer} from '@ant-design/pro-layout';
import {isSuccess} from "@/utils/utils";
import {OpResourcesManager} from "@/services/api-functions";
import ProTable, {ActionType, ProColumns} from "@ant-design/pro-table";
import {history} from "@@/core/history";
import {ExclamationCircleOutlined} from "@ant-design/icons";
import {resourceRecommend} from "@/pages/resource/list";
import {SYS_ENUMS} from "@/utils/constants";

const AllResourceList: React.FC<any> = (props) => {
  const actionRef = useRef<ActionType>();

  const columns: ProColumns[] = [
    {
      title: '标题',
      dataIndex: 'title',
      ellipsis: true,
      width: 350,
      fixed: 'left'
    },
    {
      title: '分类',
      dataIndex: 'classification',
      hideInSearch: true,
      valueEnum: SYS_ENUMS.RESOURCE_CLASSIFICATION,
      width: 150,
    },
    {
      title: '设计师',
      dataIndex: 'nickName',
      ellipsis: true,
      width: 170,
    },
    {
      title: '推荐时间',
      dataIndex: 'gmtModified',
      valueType: 'dateTime',
      width: 200,
      hideInSearch: true,
      hideInForm: true,
    },{
      title: '发布时间',
      dataIndex: 'gmtCreate',
      valueType: 'dateTime',
      width: 200,
      hideInSearch: true,
      hideInForm: true,
    },
    {
      title: '操作',
      dataIndex: 'option',
      valueType: 'option',
      hideInSearch: true,
      hideInForm: true,
      fixed: 'right',
      render: (_, record) => [
        <a key="edit" href={`/resource/detail/${record?.id}`}>详情</a>,
        <a key="edit" onClick={()=>history.push(`/resource/edit/${record.id}`)}>编辑</a>,
        record.recommend === 1
          && <a key="edit" onClick={() => Modal.confirm({
            title: '提示',
            icon: <ExclamationCircleOutlined/>,
            content: <span>确定要取消推荐 <Tag color='#108ee9'>{record.title}</Tag> 吗？</span>,
            okText: '取消推荐',
            cancelText: '取消',
            onOk: async () => {
              if (await resourceRecommend(record, 0)) {
                actionRef.current?.reload()
              }
            },
          })}>取消推荐</a>,
      ],
    },
  ];

  return (
    <PageContainer>
      <ProTable<any, any>
        actionRef={actionRef}
        rowKey="key"
        search={{
          labelWidth: 120,
        }}
        request={async (params) => {

          const query = {...params, recommend: 1}

          const {data: {data, total}, code}: any = await OpResourcesManager.pageQuery(query);

          return {data: data || [], success: isSuccess(code), total: isSuccess(code) ? total : 0};
        }}
        columns={columns}
        scroll={{ x: 1300 }}
      />
    </PageContainer>
  );
};

export default AllResourceList;
